﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace SDS.SharePoint.ReplaceDocument.Utility
{
    public static class ExtensionMethods
    {
        public static IEnumerable<SPFolder> ParentFolders(this SPFile File)
        {
            Func<SPFolder, bool> IsValidFolder = (tFolder) => tFolder != null && tFolder.Exists && !String.IsNullOrEmpty(tFolder.Url);

            System.Collections.Generic.Stack<SPFolder> tFolders = new Stack<SPFolder>();
            SPFolder tParentFolder = File.ParentFolder;
            while (IsValidFolder(tParentFolder))
            {
                // push it onto the stack
                tFolders.Push(tParentFolder);

                // check for another level
                tParentFolder = IsValidFolder(tParentFolder.ParentFolder) ? tParentFolder.ParentFolder : null;
            }

            return tFolders;
        } // IEnum<SPFolder> ParentFolders(...)

        public static string TrimStart(this string source, string comparer)
        {
            return source.StartsWith(comparer)
                 ? source.Remove(0, comparer.Length)
                 : source;
        } // string TrimStart(...)

    } // class ExtensionMethods
} // namespace
